<% vm_ds = @ps.vms_dataset.where(project_id: @ps.project_id)
viewable_vm_ds = dataset_authorize(vm_ds, "Vm:view")
displayable_vm_map, linkable_vm_map = [vm_ds, viewable_vm_ds].map! do
  it.select_hash(Sequel[:vm][:id], Sequel.as(true, :v))
end
pg_resources = dataset_authorize(@project.postgres_resources_dataset.where(private_subnet_id: @ps.id), "Postgres:view").all
kcs = dataset_authorize(@project.kubernetes_clusters_dataset.where(private_subnet_id: @ps.id), "KubernetesCluster:view").all
other_resources = pg_resources + kcs %>

<div id="private-subnet-nics" class="p-6">
  <%== part(
    "components/table_card",
    title: "Attached VMs",
    headers: ["VM", "Private IPv4", "Private IPv6"],
    empty_state: "No VM attached",
    rows:
      @ps.nics(eager: :vm).select { it.vm_id.nil? || displayable_vm_map[it.vm_id] }.map! do |nic|
        [
          [
            (
              if nic.vm
                if linkable_vm_map[nic.vm_id]
                  [nic.vm.name, { link: path(nic.vm) }]
                else
                  [nic.vm.name, {}]
                end
              else
                "-"
              end
            ),
            [nic.private_ipv4_address, { copyable: true }],
            [nic.private_ipv6_address, { copyable: true }]
          ],
          { id: "nic-#{nic.ubid}" }
        ]
      end
  ) %>

  <% unless other_resources.empty? %>
    <div class="pt-4">
      <%== part(
        "components/table_card",
        title: "Other Attached Resources",
        headers: ["Type", "Name", "ID"],
        rows:
          other_resources.map! do |resource|
            link = path(resource)
            [
              [
                [resource.is_a?(PostgresResource) ? "PostgreSQL Database" : "Kubernetes Cluster"],
                [resource.name, {link:}],
                [resource.ubid, {link:}]
              ],
            ]
          end
      ) %>
    </div>
  <% end %>
</div>
